﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class NetdiskDao
    {
        public bool Delete(SqlConnection conn, int diskId)
        {
            string commandText = "DELETE FROM [dbo].[Netdisk] WHERE [DiskId] = @DiskId ";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DiskId", SqlDbType.Int) };
            commandParameters[0].Value = diskId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
            return true;
        }

        public int Insert(SqlConnection conn, NetdiskData data)
        {
            string commandText = "INSERT INTO [dbo].[Netdisk] ( [Name], [DiskPath], [Type], [PostDept], [AccessUser], [NewUser], [ManageUser], [DefaultPriv], [ReadUser], [ReadPriv], [EditUser], [DownloadUser] ) VALUES ( @Name, @DiskPath, @Type, @PostDept, @AccessUser, @NewUser, @ManageUser, @DefaultPriv, @ReadUser, @ReadPriv, @EditUser, @DownloadUser ) SELECT SCOPE_IDENTITY() AS RetValue";
            SqlParameter[] commandParameters = new SqlParameter[12];
            commandParameters[0] = new SqlParameter("@Name", SqlDbType.NText);
            commandParameters[0].Value = data.Name;
            commandParameters[1] = new SqlParameter("@DiskPath", SqlDbType.NText);
            commandParameters[1].Value = data.DiskPath;
            commandParameters[2] = new SqlParameter("@Type", SqlDbType.Int);
            commandParameters[2].Value = data.Type;
            commandParameters[3] = new SqlParameter("@PostDept", SqlDbType.NText);
            if (data.PostDeptIsNull)
            {
                commandParameters[3].Value = DBNull.Value;
            }
            else
            {
                commandParameters[3].Value = data.PostDept;
            }
            commandParameters[4] = new SqlParameter("@AccessUser", SqlDbType.NText);
            if (data.AccessUserIsNull)
            {
                commandParameters[4].Value = DBNull.Value;
            }
            else
            {
                commandParameters[4].Value = data.AccessUser;
            }
            commandParameters[5] = new SqlParameter("@NewUser", SqlDbType.NText);
            if (data.NewUserIsNull)
            {
                commandParameters[5].Value = DBNull.Value;
            }
            else
            {
                commandParameters[5].Value = data.NewUser;
            }
            commandParameters[6] = new SqlParameter("@ManageUser", SqlDbType.NText);
            if (data.ManageUserIsNull)
            {
                commandParameters[6].Value = DBNull.Value;
            }
            else
            {
                commandParameters[6].Value = data.ManageUser;
            }
            commandParameters[7] = new SqlParameter("@DefaultPriv", SqlDbType.NText);
            if (data.DefaultPrivIsNull)
            {
                commandParameters[7].Value = DBNull.Value;
            }
            else
            {
                commandParameters[7].Value = data.DefaultPriv;
            }
            commandParameters[8] = new SqlParameter("@ReadUser", SqlDbType.NText);
            if (data.ReadUserIsNull)
            {
                commandParameters[8].Value = DBNull.Value;
            }
            else
            {
                commandParameters[8].Value = data.ReadUser;
            }
            commandParameters[9] = new SqlParameter("@ReadPriv", SqlDbType.NText);
            if (data.ReadPrivIsNull)
            {
                commandParameters[9].Value = DBNull.Value;
            }
            else
            {
                commandParameters[9].Value = data.ReadPriv;
            }
            commandParameters[10] = new SqlParameter("@EditUser", SqlDbType.NText);
            if (data.EditUserIsNull)
            {
                commandParameters[10].Value = DBNull.Value;
            }
            else
            {
                commandParameters[10].Value = data.EditUser;
            }
            commandParameters[11] = new SqlParameter("@DownloadUser", SqlDbType.NText);
            if (data.DownloadUserIsNull)
            {
                commandParameters[11].Value = DBNull.Value;
            }
            else
            {
                commandParameters[11].Value = data.DownloadUser;
            }
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, commandText, commandParameters);
            if (obj2 != null)
            {
                return Convert.ToInt32(obj2);
            }
            return 0;
        }

        public DataTable SelectAll(SqlConnection conn)
        {
            string commandText = "SELECT *, dbo.GetDeptNames(CAST([PostDept] AS nvarchar(2000))) AS PostDeptName, dbo.GetUserNames(CAST([AccessUser] AS nvarchar(4000))) AS AccessUserName, dbo.GetUserNames(CAST([AccessUser] AS nvarchar(4000))) AS AccessUserName, dbo.GetUserNames(CAST([NewUser] AS nvarchar(4000))) AS NewUserName, dbo.GetUserNames(CAST([ManageUser] AS nvarchar(4000))) AS ManageUserName, dbo.GetUserNames(CAST([ReadUser] AS nvarchar(4000))) AS ReadUserName, dbo.GetUserNames(CAST([EditUser] AS nvarchar(4000))) AS EditUserName, dbo.GetUserNames(CAST([DownloadUser] AS nvarchar(4000))) AS DownloadUserName FROM [dbo].[Netdisk]";
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText);
            set.Tables[0].TableName = "Netdisk";
            return set.Tables[0];
        }

        public DataTable SelectByDiskId(SqlConnection conn, int diskId)
        {
            string commandText = "SELECT *, dbo.GetDeptNames(CAST([PostDept] AS nvarchar(2000))) AS PostDeptName, dbo.GetUserNames(CAST([AccessUser] AS nvarchar(4000))) AS AccessUserName, dbo.GetUserNames(CAST([AccessUser] AS nvarchar(4000))) AS AccessUserName, dbo.GetUserNames(CAST([NewUser] AS nvarchar(4000))) AS NewUserName, dbo.GetUserNames(CAST([ManageUser] AS nvarchar(4000))) AS ManageUserName, dbo.GetUserNames(CAST([ReadUser] AS nvarchar(4000))) AS ReadUserName, dbo.GetUserNames(CAST([EditUser] AS nvarchar(4000))) AS EditUserName, dbo.GetUserNames(CAST([DownloadUser] AS nvarchar(4000))) AS DownloadUserName FROM [dbo].[Netdisk] WHERE [DiskId] = @DiskId ";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DiskId", SqlDbType.Int) };
            commandParameters[0].Value = diskId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Netdisk";
            return set.Tables[0];
        }

        public bool Update(SqlConnection conn, NetdiskData data)
        {
            string commandText = "UPDATE [dbo].[Netdisk] SET [Name] = @Name, [DiskPath] = @DiskPath, [Type] = @Type, [PostDept] = @PostDept, [AccessUser] = @AccessUser, [NewUser] = @NewUser, [ManageUser] = @ManageUser, [DefaultPriv] = @DefaultPriv, [ReadUser] = @ReadUser, [ReadPriv] = @ReadPriv, [EditUser] = @EditUser, [DownloadUser] = @DownloadUser WHERE [DiskId] = @DiskId ";
            SqlParameter[] commandParameters = new SqlParameter[13];
            commandParameters[0] = new SqlParameter("@DiskId", SqlDbType.Int);
            commandParameters[0].Value = data.DiskId;
            commandParameters[1] = new SqlParameter("@Name", SqlDbType.NText);
            commandParameters[1].Value = data.Name;
            commandParameters[2] = new SqlParameter("@DiskPath", SqlDbType.NText);
            commandParameters[2].Value = data.DiskPath;
            commandParameters[3] = new SqlParameter("@Type", SqlDbType.Int);
            commandParameters[3].Value = data.Type;
            commandParameters[4] = new SqlParameter("@PostDept", SqlDbType.NText);
            if (data.PostDeptIsNull)
            {
                commandParameters[4].Value = DBNull.Value;
            }
            else
            {
                commandParameters[4].Value = data.PostDept;
            }
            commandParameters[5] = new SqlParameter("@AccessUser", SqlDbType.NText);
            if (data.AccessUserIsNull)
            {
                commandParameters[5].Value = DBNull.Value;
            }
            else
            {
                commandParameters[5].Value = data.AccessUser;
            }
            commandParameters[6] = new SqlParameter("@NewUser", SqlDbType.NText);
            if (data.NewUserIsNull)
            {
                commandParameters[6].Value = DBNull.Value;
            }
            else
            {
                commandParameters[6].Value = data.NewUser;
            }
            commandParameters[7] = new SqlParameter("@ManageUser", SqlDbType.NText);
            if (data.ManageUserIsNull)
            {
                commandParameters[7].Value = DBNull.Value;
            }
            else
            {
                commandParameters[7].Value = data.ManageUser;
            }
            commandParameters[8] = new SqlParameter("@DefaultPriv", SqlDbType.NText);
            if (data.DefaultPrivIsNull)
            {
                commandParameters[8].Value = DBNull.Value;
            }
            else
            {
                commandParameters[8].Value = data.DefaultPriv;
            }
            commandParameters[9] = new SqlParameter("@ReadUser", SqlDbType.NText);
            if (data.ReadUserIsNull)
            {
                commandParameters[9].Value = DBNull.Value;
            }
            else
            {
                commandParameters[9].Value = data.ReadUser;
            }
            commandParameters[10] = new SqlParameter("@ReadPriv", SqlDbType.NText);
            if (data.ReadPrivIsNull)
            {
                commandParameters[10].Value = DBNull.Value;
            }
            else
            {
                commandParameters[10].Value = data.ReadPriv;
            }
            commandParameters[11] = new SqlParameter("@EditUser", SqlDbType.NText);
            if (data.EditUserIsNull)
            {
                commandParameters[11].Value = DBNull.Value;
            }
            else
            {
                commandParameters[11].Value = data.EditUser;
            }
            commandParameters[12] = new SqlParameter("@DownloadUser", SqlDbType.NText);
            if (data.DownloadUserIsNull)
            {
                commandParameters[12].Value = DBNull.Value;
            }
            else
            {
                commandParameters[12].Value = data.DownloadUser;
            }
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
            return true;
        }
    }
}

